using Godot;
using System.Linq;

public partial class Global : Node2D
{
    public static Global Instance { get; private set; }

    public Camera2D Camera;

    ///<summary>从摄像机当前视图所对应戳到的场景的坐标。</summary>
    public Vector2 GlobalMousePosition;

    public Node MainNode;

    public Icon Player;

    public override void _Ready()
    {
        Instance = this;
        base._Ready();

        var root = GetParent();

        var main = root.GetChildren().Last();

        if (main.HasNode("Camera2D") == false)
        {
            Camera = new Camera2D()
            {
                AnchorMode = Camera2D.AnchorModeEnum.FixedTopLeft,
            };
            AddChild(Camera);
        }
        else
        {
            Camera = main.GetNode<Camera2D>("Camera2D");
            Camera.AnchorMode = Camera2D.AnchorModeEnum.FixedTopLeft;
        }


        Player = main.GetChildren().OfType<Icon>().FirstOrDefault();

        MainNode = main;

    }

    public override void _Process(double delta)
    {
        var globalMousePosition = GetGlobalMousePosition();
        GlobalMousePosition = globalMousePosition;
        //GD.Print(globalMousePosition);

        //var mousePos = GetViewport().GetMousePosition();
        //GD.Print(mousePos);

        //var viewportRect = GetViewportRect();
        //var smallRect = viewportRect.Grow(-200);

        HandleMovement();

        base._Process(delta);
    }

    private void HandleMovement()
    {
        if (Input.IsActionJustPressed("ui_left"))
        {
            var leftPos = Vector2.Left * 200;
            var tween = GetTree().CreateTween();
            tween.TweenProperty(Camera, "offset", Camera.Offset + leftPos, 1)
                .SetEase(Tween.EaseType.Out)
                .SetTrans(Tween.TransitionType.Quint);
            //Camera.Offset = Camera.Offset + leftPos;
        }
        if (Input.IsActionJustPressed("ui_up"))
        {
            var leftPos = Vector2.Up * 200;
            var tween = GetTree().CreateTween();
            tween.TweenProperty(Camera, "offset", Camera.Offset + leftPos, 1)
                .SetEase(Tween.EaseType.Out)
                .SetTrans(Tween.TransitionType.Quint);
            //Camera.Offset = Camera.Offset + leftPos;
        }
        if (Input.IsActionJustPressed("ui_down"))
        {
            var leftPos = Vector2.Down * 200;
            var tween = GetTree().CreateTween();
            tween.TweenProperty(Camera, "offset", Camera.Offset + leftPos, 1)
                .SetEase(Tween.EaseType.Out)
                .SetTrans(Tween.TransitionType.Quint);
            //Camera.Offset = Camera.Offset + leftPos;
        }
        if (Input.IsActionJustPressed("ui_right"))
        {
            var leftPos = Vector2.Right * 200;
            var tween = GetTree().CreateTween();
            tween.TweenProperty(Camera, "offset", Camera.Offset + leftPos, 1)
                .SetEase(Tween.EaseType.Out)
                .SetTrans(Tween.TransitionType.Quint);
            //Camera.Offset = Camera.Offset + leftPos;
        }
    }

    public override void _EnterTree()
    {
        base._EnterTree();
    }

    public override void _ExitTree()
    {
        base._ExitTree();
    }



}
